package com.xmad.ksadutils.AdSDK.AdMold.Pangolin;


import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdManager;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTNativeExpressAd;
import com.xmad.ksadutils.AdSDK.AdBase.AdBaseLoad;
import com.xmad.ksadutils.AdSDK.AdBase.AdCode;
import com.xmad.ksadutils.AdSDK.AdBase.TTAdManagerHolder;
import com.xmad.ksadutils.AdSDK.Interface.AdLoad;
import com.xmad.ksadutils.AdSDK.Utlis.RxLogUtils;
import com.xmad.ksadutils.AdSDK.Utlis.Tool;

import java.util.List;

public class PGloadExpressDrawFeedAd extends AdBaseLoad implements AdLoad {
    private volatile static PGloadExpressDrawFeedAd mInstance;
    public TTNativeExpressAd ttNativeExpressAd;
    /**
     * 单例模式
     *
     * @return
     */
    public static PGloadExpressDrawFeedAd getInstance() {
        if (mInstance == null) {
            synchronized (PGloadExpressDrawFeedAd.class) {
                if (mInstance == null) {
                    mInstance = new PGloadExpressDrawFeedAd();
                }
            }
        }
        return mInstance;
    }

    @Override
    public void Show(Activity activity, final FrameLayout frameLayout) {
        if (AdCode.getKsLoadFeedAdId().equals("")&&PGAdId==null) {
            RxLogUtils.e("还未设置ID");
            return;
        }
        TTAdManager ttAdManager = TTAdManagerHolder.get();
        //step2:(可选，强烈建议在合适的时机调用):申请部分权限，如read_phone_state,防止获取不了imei时候，下载类广告没有填充的问题。
        // TTAdManagerHolder.get().requestPermissionIfNecessary(context);
        //step3:创建TTAdNative对象,用于调用广告请求接口
        TTAdNative TTAdNative = ttAdManager.createAdNative(activity);
        if (ToastCode){
            Toast.makeText(activity,!PGAdId.equals("") ? PGAdId: AdCode.getFeedAdId(), Toast.LENGTH_SHORT).show();
        }
        AdSlot adSlot = new AdSlot.Builder()
                .setCodeId(!PGAdId.equals("") ? PGAdId: AdCode.getFeedAdId())
                .setExpressViewAcceptedSize(Tool.getScreenWidth(activity), Tool.getScreenHeight(activity)) //期望模板广告view的size,单位dp
                .setAdCount(1) //请求广告数量为1到3条
                .build();
        //step4:请求广告,对请求回调的广告作渲染处理
        TTAdNative.loadExpressDrawFeedAd(adSlot, new TTAdNative.NativeExpressAdListener() {
            @Override
            public void onError(int code, String msg) {
                if (listener!=null){
                    listener.onError(code+msg);
                }
            }

            @Override
            public void onNativeExpressAdLoad(final List<TTNativeExpressAd> ads) {
                if (ads == null || ads.isEmpty()) {
                    // TToast.show(DrawNativeExpressVideoActivity.this, " ad is null!");
                    return;
                }
                for (TTNativeExpressAd ad : ads) {
                    //点击监听器必须在getAdView之前调
                    ttNativeExpressAd=ad;
                    ad.setVideoAdListener(new TTNativeExpressAd.ExpressVideoAdListener() {
                        @Override
                        public void onVideoLoad() {
                            Log.d(TAG, "onVideoLoad ");

                        }

                        @Override
                        public void onVideoError(int Code, int extraCode) {
                            Log.d(TAG, "onVideoError ");
                            listener.onError(Code+"");
                        }

                        @Override
                        public void onVideoAdStartPlay() {
                            Log.d(TAG, "onVideoAdStartPlay ");
                        }

                        @Override
                        public void onVideoAdPaused() {
                            Log.d(TAG, "onVideoAdPaused ");
                        }

                        @Override
                        public void onVideoAdContinuePlay() {
                            Log.d(TAG, "onVideoAdContinuePlay ");
                        }

                        @Override
                        public void onProgressUpdate(long current, long duration) {
                            Log.d(TAG, "onProgressUpdate ");
                        }

                        @Override
                        public void onVideoAdComplete() {
                            Log.d(TAG, "onVideoAdComplete ");
                        }

                        @Override
                        public void onClickRetry() {
                            // TToast.show(DrawNativeExpressVideoActivity.this, " onClickRetry !");
                            Log.d("drawss", "onClickRetry!");
                        }
                    });
                    ad.setCanInterruptVideoPlay(true);
                    ad.setExpressInteractionListener(new TTNativeExpressAd.ExpressAdInteractionListener() {
                        @Override
                        public void onAdClicked(View view, int type) {
                            Log.d(TAG, "onAdClicked ");
                            if (listener!=null){
                                listener.onAdClicked();
                            }
                        }

                        @Override
                        public void onAdShow(View view, int type) {
                            Log.d(TAG, "onAdShow ");
                        }

                        @Override
                        public void onRenderFail(View view, String msg, int code) {
                            Log.d(TAG, "onRenderFail ");
                        }

                        @Override
                        public void onRenderSuccess(View view, float width, float height) {
                            //TToast.show(DrawNativeExpressVideoActivity.this, "渲染成功");
                            // ads.get(0).getExpressAdView();
                            Log.d(TAG, "onRenderSuccess");
                            frameLayout.removeAllViews();
//                            if(view.getParent()!=null){
//                                ((ViewGroup)view.getParent()).removeView(view);
//                            }
                            frameLayout.addView(ads.get(0).getExpressAdView());
                        }
                    });
                    ad.render();
                }
            }
        });
    }




}
